package com.yuanduan.mplus.view;


import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.cj.framework.app.BaseActivity;
import com.cj.framework.util.HttpImp;
import com.cj.framework.util.IHttp;
import com.cj.framework.util.RSAUtils;
import com.yuanduan.mplus.R;
import com.yuanduan.mplus.config.Config;
import com.yuanduan.mplus.model.DBManager;
import com.yuanduan.mplus.model.entity.CommentsReceivedEntity;
import com.yuanduan.mplus.model.entity.User;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 收到的点评界面
 */
public class CommentsReceivedActivity extends BaseActivity {
    private Toolbar toolbar;
    private ListView listView;
    private User user;
    private List<CommentsReceivedEntity> datas;
    private MCommentsReceivedAdapter adapter;
    private IHttp iHttp=new HttpImp();
    private IHttp.HttpInfoCallback callback=new IHttp.HttpInfoCallback() {
        @Override
        public void onSucceed(Object result) {
            //解析结果到-》datas
            datas.clear();
            ArrayList<String> time=new ArrayList<>();
            ArrayList<String> content=new ArrayList<>();
            ArrayList<String> acceptName=new ArrayList<>();
            ArrayList<String> pushName=new ArrayList<>();
            String data= RSAUtils.decryptWithRSA((String) result);
            try {
                JSONObject jsonObject=new JSONObject(data);
                if(jsonObject.getInt("RS")!=1){
                    return;
                }else{
                    JSONArray timeArray=jsonObject.getJSONArray("CreationTime");
                    JSONArray acceptRealName=jsonObject.getJSONArray("AcceptRealName");
                    JSONArray pushRealName=jsonObject.getJSONArray("OriginatorRealName");
                    JSONArray message=jsonObject.getJSONArray("Message");
                    for(int i=0;i<timeArray.length();i++){
                        time.add(timeArray.getString(i));
                        content.add(message.getString(i));
                        acceptName.add(acceptRealName.getString(i));
                        pushName.add(pushRealName.getString(i));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            if(Config.userType==1){
                for(int i=0;i<time.size();i++){
                    CommentsReceivedEntity entity=new CommentsReceivedEntity();
                    entity.setPerson("点评人:"+pushName.get(i));
                    entity.setContent("点评内容:"+content.get(i));
                    entity.setTime(new Date(Long.getLong(time.get(i))*1000).toString());
                    datas.add(entity);
                }
            }else if(Config.userType==2){
                for(int i=0;i<time.size();i++){
                    CommentsReceivedEntity entity=new CommentsReceivedEntity();
                    entity.setPerson("点评对象:"+pushName.get(i));
                    entity.setContent("点评内容:"+content.get(i));
                    entity.setTime(new Date(Long.getLong(time.get(i))*1000).toString());
                    datas.add(entity);
                }
            }else{
                CommentsReceivedEntity entity=new CommentsReceivedEntity();
                entity.setTime("2016-1-1 10:10:10");
                entity.setContent("小伙子不错，有钱途");
                entity.setPerson("路人甲");
                datas.add(entity);
                CommentsReceivedEntity entity2=new CommentsReceivedEntity();
                entity2.setTime("2016-1-2 12:12:12");
                entity2.setContent("小伙子,你似不似捡到钱了，过来聊一聊");
                entity2.setPerson("路人已");
                datas.add(entity2);
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFailed(Exception e) {

        }
    };

    @Override
    protected void findId() {
        toolbar= (Toolbar) findViewById(R.id.toolbar);
        listView= (ListView) findViewById(R.id.commentsreceived_listview);
    }

    @Override
    protected void logic() {
        if(Config.userType==1) {
            toolbar.setTitle("我收到的点评");
        }else if(Config.userType==2){
            toolbar.setTitle("管理员点评管理");
        }else{
            //游客登陆
        }
        user= DBManager.getInstance(getApplicationContext()).getUser();
        toolbar.setNavigationIcon(R.mipmap.back_arrow_black);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommentsReceivedActivity.this.finish();
            }
        });
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){
                    case R.id.commentsreceived_menu_comment:
                        openActivity(CommentsActivity.class);
                        break;
                }
                return true;
            }
        });
        datas=new ArrayList<>();
        adapter=new MCommentsReceivedAdapter();
        listView.setAdapter(adapter);
        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("UserPhone",user.getUserPhone());
        hashMap.put("UserPassword",user.getUserPwd());
        if(Config.userType==1){
            hashMap.put("isAdmin","no");
        }else if(Config.userType==2){
            hashMap.put("isAdmin","yes");
        }
        long  timer=System.currentTimeMillis()/1000-60*60*24*7;
        hashMap.put("timer",timer+"");
        iHttp.httpInfoPost(Config.URL+Config.COMMENTS,Config.translateMap(hashMap),callback);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if(Config.userType==2){
            getMenuInflater().inflate(R.menu.menu_commentsreceived,menu);
            return true;
        }else{
            return super.onCreateOptionsMenu(menu);
        }
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_comments_received;
    }

    class MCommentsReceivedAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int position) {
            return  datas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            CommentsReceivedEntity entity=datas.get(position);
            View view;
            ViewHolder holder;
            if(convertView==null){
                view= LayoutInflater.from(CommentsReceivedActivity.this).inflate(R.layout.commentsreceived_listview,null);
                holder=new ViewHolder();
                holder.tv_content= (TextView) view.findViewById(R.id.commentsreceived_tv_content);
                holder.tv_person= (TextView) view.findViewById(R.id.commentsreceived_tv_person);
                holder.tv_time= (TextView) view.findViewById(R.id.commentsreceived_tv_time);
                view.setTag(holder);
            }else{
                view=convertView;
                holder= (ViewHolder) view.getTag();
            }
            holder.tv_content.setText(entity.getContent());
            holder.tv_person.setText(entity.getPerson());
            holder.tv_time.setText(entity.getTime());
            return view;
        }
        class ViewHolder{
             TextView tv_person;
             TextView tv_content;
             TextView tv_time;
        }
    }
}
